import requests
import re


# def get_old_view_count(video_url):
#     for i in range(5):
#         try:
#             res = requests.get(
#                 url=video_url,
#                 headers={
#                     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
#                     "referer": "https://m.yangshipin.cn/"
#                 }
#             )
#             match_object = re.findall(r'"subtitle":"(.+)次观看","', res.text)
#             if not match_object:
#                 return True, 0
#             return True, match_object[0]
#         except Exception as e:
#             pass
#     return False, 0


# if __name__ == '__main__':
#     # count = get_old_view_count("https://w.yangshipin.cn/video?type=0&vid=y000088hru8")
#     count = get_old_view_count("https://w.yangshipin.cn/video?type=0&vid=f0000711h22")
#     print(count)




def get_old_view_count(video_url):
    for i in range(5):  # 如果出现异常了 就重试5次
        try:
            # cookie = 'BAIDUID=03F0FD538A19FE80160C1EDD0BCD2EA7:FG=1; BIDUPSID=3FFC91E20CE9890EF3DC5D68B89B9DD0; PSTM=1705391111; MCITY=-176%3A; H_PS_PSSID=40080_40463; H_WISE_SIDS=40080_40463; H_WISE_SIDS_BFESS=40080_40463; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=03F0FD538A19FE80160C1EDD0BCD2EA7:FG=1; BA_HECTOR=258h24ak8k8g8g80240g0l057d4k0m1j2soeg1s; ZFY=wHmtdP1Y2K0iMksVOvYvybcMdeCRAnChQWs4to:Br:ByM:C; PC_TAB_LOG=video_details_page; COMMON_LID=ec9c53ca960fc246d552c545498c9d19; ab_sr=1.0.1_YzFkMWJkZjRhNGI5YWIzNmUxNzNiMGE1Mzg1OWVjMzM2NDc5NTBiMzIzZWE2MTM0ZGJlYzA1MGNjOWMzOWJkMzRiZjcwNTkwMzI1MTNiMDk2NWI0YTI1ODU4OGU2ZDE4OTI0YTJkZmMwMzQ0ZGU5YzdmYzNkYzBiNTBkYzZhMGY3NDJkZjYzZDBiOGY5MmZkNDFlODZhNTBiNjZlMjFiM2VkZDI5OWZjYTBmOTk5ODljZGY2N2YyZWMxNWIwMzRiOWIwMTc2OGQ1YWE2ZDk0YjI1ODI3Yzk3NTc3NDFlNzk=; reptileData=%7B%22data%22%3A%22b4937c741acc9bcadb8e4b7e036b3bdc0b4069ce343a50294e8dfb9f5def7dad36743ce0158037a39a80d5b143d84c86e69354752a300f3057a9c6923094d4270e0b74300c4984f644d10f858676ebeddf43a9d20f1aaa1cd8edc12627aa640b16318764e230ad4f11f1fb53044dc0f5545cd205d1094448acfad81dfaa573eb%22%2C%22key_id%22%3A%2230%22%2C%22sign%22%3A%221672ecee%22%7D; Hm_lvt_4aadd610dfd2f5972f1efee2653a2bc5=1714297537,1714297541,1714315653,1714352465; Hm_lpvt_4aadd610dfd2f5972f1efee2653a2bc5=1714352465; RT="z=1&dm=baidu.com&si=2f2b4eb2-e511-466d-95f4-a1e606b28711&ss=lvk96y3d&sl=0&tt=0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=vpe&ul=1icz"'
            # cookie = 'guid=lvxmmnnn_9nv2lhy18ug; versionName=99.99.99; versionCode=999999; vplatform=109; platformVersion=Chrome; deviceModel=124; videoHistory_nouser=%5B%7B%22vid%22%3A%22k000023a5b5%22%2C%22cid%22%3A%22%22%2C%22recordTime%22%3A11.464444%7D%5D; seqId=13; request-id=999999cIGxZXbVw91715164563491'
            res = requests.get(
                url=video_url,
                headers={
                    # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0',
                    # 'referer': 'https://haokan.baidu.com/'
                    'referer': 'https://m.yangshipin.cn/',
                },
                # cookies={
                #     'Cookie': cookie
                # }
            )
            # match_object = re.findall(r'(\d+)次播放', res.text) # 好看视频
            match_object = re.findall(r'"subtitle":"(.+)次观看","', res.text)
            if not match_object:
                return True, 0
            return True, match_object[0]
        except Exception as e:
            pass
    return False, 0


if __name__ == '__main__':
    # count = get_old_view_count("https://haokan.baidu.com/v?vid=9895466177296554639&tab=recommend")
    count = get_old_view_count("https://w.yangshipin.cn/video?type=0&vid=f0000711h22")
    print(count)
